package com.tengfei.dynamicProxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Description:
 * @author: ten_fei
 * @date: 2021-07-09 15:33
 */
public class InvocationHandlerUtil implements InvocationHandler {
    // 被代理的类
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    // 生成得到代理类
    public Object getProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    // 处理代理实例，并返回结果

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        log(method.getName());

        Object result = method.invoke(target, args);

        return result;
    }

    public void log(String msg) {
        System.out.println("执行了" + msg + "方法");
    }
}
